package com.douqu.game.auth.web.request;

import com.alibaba.fastjson.JSONObject;
import com.bean.core.util.MD5Utils;
import com.douqu.game.core.factory.PlatformFactory;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.PayUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by dengbo on 2018/9/13 0013.
 */
public class PPCheckOrderReqParam {
    private String OrderId;
    private String BillNo;
    private String UserCode;
    private String GameId;
    private String Amount;
    private String ProductId;
    private String RoleId;
    private String ServerId;
    private String Status;
    private String ExtraInfo;
    private String Sign;

    private boolean isResultOk;

    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "OrderId=L201811131724153828f7290" +
                "&BillNo=51020082DY4E9_8_5_1_60_263185_PLACEHOLDER_PLACEHOLDER_RE20181113173109Y427" +
                "&UserCode=51Gamer15417323600a4fd0b126&GameId=27&Amount=6.00" +
                "&ProductId=com.pp.blbz.szzb0008&RoleId=51020082DY4E9" +
                "&ServerId=%E4%B8%80%E9%A9%AC%E5%B9%B3%E5%B7%9D&Status=0" +
                "&ExtraInfo=51020082DY4E9_8_5_1_60_263185_PLACEHOLDER_PLACEHOLDER_RE20181113173109Y427";
        String Sign = "ad5a4dd98ab37e41033df5956071b39b";
        PlatformFactory.PP_PAY_KEY = "6b2caeec98e11b2359f1d14adb0eb457";
        JSONObject param = new JSONObject();
        param.put("OrderId", "L2018111418551930969503f");
        param.put("BillNo", "51020082DY4E9_8_5_1_60_263185_PLACEHOLDER_PLACEHOLDER_RE20181114190204DIND");
        param.put("UserCode", "51Gamer15417323600a4fd0b126");
        param.put("ProductId", "com.pp.blbz.szzb0008");
        String id = URLDecoder.decode("%E4%B8%80%E9%A9%AC%E5%B9%B3%E5%B7%9D", "UTF-8");
        System.out.println("id:" + id);
        param.put("ServerId", id);
        param.put("ExtraInfo", "51020082DY4E9_8_5_1_60_263185_PLACEHOLDER_PLACEHOLDER_RE20181114190204DIND");
        param.put("RoleId", "51020082DY4E9");
        param.put("GameId", "27");
        param.put("Amount", "6.00");
        param.put("Status", "0");
        String code = PayUtils.createLinkString(param, "&") + "&PayKey=" + PlatformFactory.PP_PAY_KEY;
        System.out.println("code -> " + code);
        String encode = MD5Utils.encode(code);
        System.out.println("encode -> " + encode);
    }
    public void init(HttpServletRequest request)
    {
        try{
            JSONObject param = new JSONObject();
            param.put("OrderId", OrderId = request.getParameter("OrderId"));
            param.put("BillNo", BillNo = request.getParameter("BillNo"));
            param.put("UserCode", UserCode = request.getParameter("UserCode"));
            param.put("ProductId", ProductId = request.getParameter("ProductId"));
            ServerId = URLDecoder.decode(request.getParameter("ServerId"), "UTF-8");
            LogUtils.info("ServerId -> " + ServerId);
            param.put("ServerId", ServerId);
            param.put("ExtraInfo", ExtraInfo = request.getParameter("ExtraInfo"));
            param.put("RoleId", RoleId = request.getParameter("RoleId"));
            param.put("GameId", GameId = request.getParameter("GameId"));
            param.put("Amount", Amount = request.getParameter("Amount"));
            param.put("Status", Status = request.getParameter("Status"));

            Sign = request.getParameter("Sign");
            String source = PayUtils.createLinkString(param, "&") + "&PayKey=" + PlatformFactory.PP_PAY_KEY;
            LogUtils.info("source -> " + source);
            String localSign = MD5Utils.encode(source);
            LogUtils.info("pp本地秘钥：" + localSign + " 传过来的密钥:" + Sign);
            isResultOk = localSign.equalsIgnoreCase(Sign);

        }catch (Exception e){
            LogUtils.errorException(e);
        }
    }

    public boolean isResultOk() {
        return isResultOk;
    }

    public boolean isSuccess(){
        return Status.equals("0");
    }

    public String getBillNo() {
        return BillNo;
    }

    public String getServerId() {
        return ServerId;
    }

    public String getExtraInfo() {
        return ExtraInfo;
    }

    public String getOrderId() {
        return OrderId;
    }

    public String getUserCode() {
        return UserCode;
    }

    public String getGameId() {
        return GameId;
    }

    public String getAmount() {
        return Amount;
    }

    public String getProductId() {
        return ProductId;
    }

    public String getRoleId() {
        return RoleId;
    }

    public String getSign() {
        return Sign;
    }

    public String getStatus() {
        return Status;
    }

    @Override
    public String toString() {
        return "PPCheckOrderReqParam{" +
                "OrderId='" + OrderId + '\'' +
                ", BillNo='" + BillNo + '\'' +
                ", UserCode='" + UserCode + '\'' +
                ", GameId='" + GameId + '\'' +
                ", Amount='" + Amount + '\'' +
                ", ProductId='" + ProductId + '\'' +
                ", RoleId='" + RoleId + '\'' +
                ", ServerId='" + ServerId + '\'' +
                ", Status='" + Status + '\'' +
                ", ExtraInfo='" + ExtraInfo + '\'' +
                ", Sign='" + Sign + '\'' +
                ", isResultOk=" + isResultOk +
                '}';
    }
}
